const Koa = require('koa');
const koaBody = require('koa-body');//处理body参数
const koaStatic = require('koa-static');
const error = require('koa-json-error');//处理错误
const parameter = require('koa-parameter');//传入参数过滤处理
const mongoose = require('mongoose');//连接mongoose
const path = require('path');//自带处理文件路径
const app = new Koa();
const routing = require('./routes');
const { connectionStr } = require('./config')

mongoose.connect(connectionStr, {useUnifiedTopology:true,useNewUrlParser:true} , () => console.log('MongoDB 连接成功了!'));
mongoose.connection.on('error', console.error);

app.use(koaStatic(path.join(__dirname, 'public')));
app.use(error({
    postFormat:(e, {stack, ...rest}) => process.env.NODE_ENV === 'production' ? rest : { stack, ...rest }
}));
app.use(koaBody({
    multipart: true,
    formidable:{
        uploadDir: path.join(__dirname, '/public/uploads'),
        keepExtensions:true,
    }
}));
app.use(parameter(app));
routing(app);


app.listen(3001, () => console.log('程序启动在 3001 端口了'));